package kr.co.icebird.spring3.config.util;

import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.MultiplePiePlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.util.TableOrder;

public class ChartBuilder {
	
	public enum ChartType{ BAR, LINE, MPAI }
	
	byte[] imageByte;
	
	JFreeChart chart;
	
	String title;
	String xLabel;
	String yLabel;
	
	DefaultCategoryDataset categoryDataset;
	
	public ChartBuilder(){
		categoryDataset = new DefaultCategoryDataset();
	
	}
	/**
	 * 파이챠트의 경우는 카테고리를 가운데로 처리한다.
	 * @param catagory
	 * @param key
	 * @param value
	 */
	public void addValue(String catagory, String key, Number value){
		categoryDataset.addValue(value, catagory, key);
	}

	private void createChartBarImage() {		
		chart = ChartFactory.createBarChart(title, xLabel, yLabel, categoryDataset, PlotOrientation.VERTICAL, true, true, false);
		CategoryPlot plot = (CategoryPlot)chart.getPlot();
		// 가로축 제목및 레이블
		plot.getDomainAxis().setLabelFont(new Font("Batang", plot.getDomainAxis().getLabelFont().getStyle(), plot.getDomainAxis().getLabelFont().getSize()));
		plot.getDomainAxis().setTickLabelFont(new Font("Batang", plot.getDomainAxis().getTickLabelFont().getStyle(), plot.getDomainAxis().getTickLabelFont().getSize()));
		// 세로축 제목및 레이블
		plot.getRangeAxis().setLabelFont(new Font("Batang", plot.getRangeAxis().getLabelFont().getStyle(), plot.getRangeAxis().getLabelFont().getSize()));
		plot.getRangeAxis().setTickLabelFont(new Font("Batang", plot.getRangeAxis().getTickLabelFont().getStyle(), plot.getRangeAxis().getTickLabelFont().getSize()));
	}


	private void createChartLineImage() {
		chart = ChartFactory.createLineChart(title, xLabel, yLabel, categoryDataset, PlotOrientation.VERTICAL, true, true, true);
		CategoryPlot plot = (CategoryPlot)chart.getPlot();

		// 가로축 제목및 레이블
		plot.getDomainAxis().setLabelFont(new Font("Batang", plot.getDomainAxis().getLabelFont().getStyle(), plot.getDomainAxis().getLabelFont().getSize()));
		plot.getDomainAxis().setTickLabelFont(new Font("Batang", plot.getDomainAxis().getTickLabelFont().getStyle(), plot.getDomainAxis().getTickLabelFont().getSize()));
		// 세로축 제목및 레이블
		plot.getRangeAxis().setLabelFont(new Font("Batang", plot.getRangeAxis().getLabelFont().getStyle(), plot.getRangeAxis().getLabelFont().getSize()));
		plot.getRangeAxis().setTickLabelFont(new Font("Batang", plot.getRangeAxis().getTickLabelFont().getStyle(), plot.getRangeAxis().getTickLabelFont().getSize()));
	}
	
	private void createChartMPaiImage() {
		chart = ChartFactory.createMultiplePieChart(title, categoryDataset, TableOrder.BY_COLUMN, true, true, true);
		MultiplePiePlot plot = (MultiplePiePlot)chart.getPlot();
		plot.getPieChart().getTitle().setFont(new Font("Batang", Font.BOLD, 20));
		
		PiePlot p = (PiePlot)plot.getPieChart().getPlot();
		p.setLabelFont(new Font("Batang", Font.PLAIN, 12));
	}
	
	public byte[] createChartImage(ChartType chartType, String sTitle, String sXLabel, String sYLabel) throws Exception{
		title = sTitle;
		xLabel = sXLabel;
		yLabel = sYLabel;
		if (chartType.equals(ChartType.MPAI)) {
			createChartMPaiImage();
		} else if (chartType.equals(ChartType.BAR) ) {
			createChartBarImage();
		} else if (chartType.equals(ChartType.LINE) ) {
			createChartLineImage();
		} else {
			throw new Exception("생성가능한 챠트타입이 아닙니다.");
		}
		chart.getTitle().setFont(new Font("Batang", Font.BOLD, 20));
		chart.getLegend().setItemFont(new Font("Batang", chart.getLegend().getItemFont().getStyle(), chart.getLegend().getItemFont().getSize()));
		BufferedImage br = chart.createBufferedImage(800, 400);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(br, "PNG", baos);
		baos.flush();
		return baos.toByteArray();
	}
	
	public String createChartFile(ChartType chartType, String sTitle, String sXLabel, String sYLabel, String fileName) throws Exception{
		
		if(fileName == null){
			throw new Exception("파일명이 없습니다.");
		}
		
		FileOutputStream FOS = new FileOutputStream(fileName);	
		FOS.write(createChartImage(chartType, sTitle, sXLabel, sYLabel));
		FOS.close();
		return fileName;
	}
	
}
